package com.example.multiReactorMultiThread;

import com.example.CommonMethod;
import lombok.Data;

import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;

@Data
public class Handler implements Runnable {

    private ExecutorService executorService;
    private SocketChannel socketChannel;
    private SelectionKey selectionKey;


    public Handler(SocketChannel socketChannel,  ExecutorService executorService) {
        this.socketChannel = socketChannel;
        this.executorService = executorService;
    }


    @Override
    public void run() {
        if (selectionKey!=null){
            if (selectionKey.isReadable()) {
                executorService.submit(()-> CommonMethod.read(selectionKey));
            } else if (selectionKey.isWritable()) {
                executorService.submit(()-> CommonMethod.write(selectionKey));
            }
        }

    }

}
